package com.yc.bran.snack.service.impl;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.yc.bran.snack.Repository.CommentRepository;
import com.yc.bran.snack.Repository.ReplyCommentRepository;
import com.yc.bran.snack.bean.Comment;
import com.yc.bran.snack.bean.ReplyComment;
import com.yc.bran.snack.dto.CommentDTO;
import com.yc.bran.snack.dto.ReplyCommentDTO;

@Service  
public class CommentServiceImpl { 

	@Autowired  
	private CommentRepository commentRepository;
	
	@Autowired  
	private ReplyCommentRepository replyCommentRepository;
	
	@Autowired  
    private StringRedisTemplate stringRedisTemplate;  

	// 根据商品gid查询评论数据
	public List<Comment> getCommentsByGid(Integer gid) { 
		List<Comment> comments = commentRepository.findByGid(gid);  
        if (comments == null) {  
            return new ArrayList<>();  
        }
        
        for (Comment comment : comments) {  
            String avatarKey = "user:avatarUrl:" + comment.getCommentPrId();
            System.out.println(avatarKey);
            String avatarUrl = stringRedisTemplate.opsForValue().get(avatarKey);
            System.out.println(avatarUrl);
            if (avatarUrl != null) {  
                comment.setCommentUserAvatar(avatarUrl);
            }  
        }  
        return comments;  
    }    
	
	// 根据商品gid查询回复评论数据
	public List<ReplyComment> getReplycommentsByGid(Integer gid) {  
		List<ReplyComment> replycomments = replyCommentRepository.findByGid(gid);  
        if (replycomments == null) {  
            return new ArrayList<>();  
        }
        
        for (ReplyComment replycomment : replycomments) {  
            String avatarKey = "user:avatarUrl:" + replycomment.getReplycommentPrId();
            System.out.println(avatarKey);
            String avatarUrl = stringRedisTemplate.opsForValue().get(avatarKey);
            System.out.println(avatarUrl);
            if (avatarUrl != null) {  
            	replycomment.setReplycommentUserAvatar(avatarUrl);
            }  
        }  
        return replycomments;  
	} 

	// 添加评论
	public Comment addComment(CommentDTO commentDTO) {  
		Comment comment = new Comment();
		comment.setCommentPrId(commentDTO.getCommentPrId());
		comment.setGid(commentDTO.getGid());
		comment.setCommentUserName(commentDTO.getCommentUserName());  
		comment.setCommentText(commentDTO.getCommentText());  
		 
		comment.setCommentTime(LocalDate.now());
		
		return commentRepository.save(comment);  
	}
	
	// 添加回复评论
	public ReplyComment addReplyComment(ReplyCommentDTO replycommentDTO) {  
		ReplyComment replyComment = new ReplyComment();
		replyComment.setReplycommentPrId(replycommentDTO.getReplycommentPrId());
		replyComment.setGid(replycommentDTO.getGid());
		replyComment.setReplycommentUserName(replycommentDTO.getReplycommentUserName()); 
		replyComment.setReplycommentText(replycommentDTO.getReplycommentText());
		replyComment.setReplycommentTime(LocalDate.now());
		replyComment.setParentId(replycommentDTO.getParentId());
		replyComment.setReplyName(replycommentDTO.getReplyName());
		
		return replyCommentRepository.save(replyComment);
	}
	
	// 修改头像
	public Comment updateAvatar(Integer commentPrId, String upic) {  
		Comment comment = commentRepository.findBycommentPrId(commentPrId);
        if (comment != null) {  
        	comment.setCommentUserAvatar(upic);
            return commentRepository.save(comment);  
        }  
        return null;  
    }  
}